import axios from 'axios';
import * as actionTypes from './actionTypes';
import { fromJS } from 'immutable'; 

const initHomeData = (result) => ({
	type: actionTypes.INIT_HOME_DATA,
	topicList: result.topicList,
	articleList: result.articleList,
	recommendList: result.recommendList
})

const addHomeData = (list, nextPage) => ({
	type: actionTypes.ADD_HOME_DATA,
	list: fromJS(list),
	nextPage
})

export const getInitHomeData = () => {
	return (dispatch) => {
		axios.get('/mock/home.json').then(res => {
			const result = res.data;
			dispatch(initHomeData(result));
		}).catch(err => {
			console.log('请求失败');
		})
	}
};

export const getMoreList = (page) => {
	return (dispatch) => {
		axios.get('/mock/homeList.json?page=' + page).then(res => {
			const result = res.data.data;
			dispatch(addHomeData(result, page + 1));
		}).catch(err => {
			console.log('请求失败');
		})
	}
}

export const toggleTopShow = (show) => ({
	type: actionTypes.TOGGLE_SCROLL_TOP,
	show
})